package com.haixiaoke.saas.order.state.role;

import com.haixiaoke.saas.common.enums.OrderStateEnum;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.spring.SpringUtils;
import com.haixiaoke.saas.house.service.IHouseService;
import com.haixiaoke.saas.house.service.impl.HouseServiceImpl;
import com.haixiaoke.saas.houseMember.service.IHouseMemberService;
import com.haixiaoke.saas.houseMember.service.impl.HouseMemberServiceImpl;
import com.haixiaoke.saas.lock.service.IHouseLockService;
import com.haixiaoke.saas.lock.service.ILockFaceService;
import com.haixiaoke.saas.lock.service.ILockFreezeLogService;
import com.haixiaoke.saas.lock.service.impl.HouseLockServiceImpl;
import com.haixiaoke.saas.order.domain.Order;
import com.haixiaoke.saas.order.domain.OrderStateRecord;
import com.haixiaoke.saas.order.service.*;
import com.haixiaoke.saas.order.service.impl.*;
import com.haixiaoke.saas.order.state.server.IOrderStateServerRole;
import com.haixiaoke.saas.order.state.server.ISpecificOrderStateServerRole;
import com.haixiaoke.saas.setSwitch.service.ISetSwitchService;
import com.haixiaoke.saas.setSwitch.service.impl.SetSwitchServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 订单状态服务方-抽象类
 * 此类为订单默认实现方法，通常用于返回默认异常值
 *
 * @author qixi
 * @date 2024-4-2 15:18:23
 */
public abstract class AbstractSpecificOrderOrderStateServerRole implements ISpecificOrderStateServerRole {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractSpecificOrderOrderStateServerRole.class);
    public IOrderService orderService = SpringUtils.getBean(OrderServiceImpl.class);
    public IOrderStateRecordService orderStateRecordService = SpringUtils.getBean(OrderStateRecordServiceImpl.class);
    public ISetSwitchService orderAuditingService = SpringUtils.getBean(SetSwitchServiceImpl.class);
    public IOrderAuditRecordService orderAuditRecordService = SpringUtils.getBean(OrderAuditRecordServiceImpl.class);
    public IOrderLesseeService orderLesseeService = SpringUtils.getBean(OrderLesseeServiceImpl.class);
    public IHouseMemberService houseMemberService = SpringUtils.getBean(HouseMemberServiceImpl.class);
    public IHouseService houseService = SpringUtils.getBean(HouseServiceImpl.class);
    public IHouseLockService houseLockService = SpringUtils.getBean(HouseLockServiceImpl.class);
    public ILockFaceService lockFaceService = SpringUtils.getBean(ILockFaceService.class);
    public ILockFreezeLogService lockFreezeLogService = SpringUtils.getBean(ILockFreezeLogService.class);
    protected IOrderStateServerRole stateServerRole;

    public AbstractSpecificOrderOrderStateServerRole(IOrderStateServerRole stateServerRole) {
        this.stateServerRole = stateServerRole;
    }

    private static String getLabel(Order order) {
        String orderState = order.getOrderState();
        OrderStateEnum orderStateEnum = OrderStateEnum.from(orderState);
        String label = orderStateEnum.getLabel();
        return label;
    }

    @Override
    public void pay(Order order) {
        // 默认实现
        LOG.info("订单状态异常无法支付 订单状态{}", order.getOrderState());
    }

    @Override
    public void sign(Order order) {
        throw new ServiceException("订单状态异常无法入住");
    }

    @Override
    public void signAudit(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法签约！");
    }

    @Override
    public void approved(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",审核！");
    }

    @Override
    public void disagree(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",审核！");
    }

    @Override
    public void changeOrder(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }

    @Override
    public void cancel(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法作废！");
    }

    @Override
    public void refundRent(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法退租！");
    }

    @Override
    public void refundRentAudit(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法退租！");
    }

    @Override
    public void relet(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法续租！");
    }

    public void addOrderStateRecord(String orderId, String beforeState, String afterState) {
        OrderStateRecord orderStateRecord = new OrderStateRecord();
        orderStateRecord.setOrderId(orderId);
        orderStateRecord.setOrderBeforeState(beforeState);
        orderStateRecord.setOrderAfterState(afterState);
        // if (ObjectUtil.isNotNull(loginUser)) {
        //     LoginUser loginUser = getLoginUser();
        //     orderStateRecord.setOperatorId(loginUser.getUserId());
        // }
        orderStateRecordService.insertOrderStateRecord(orderStateRecord);
    }

    public void addOrderStateRecord(String orderId, String beforeState, String afterState,String recordSource,Long operatorId) {
        OrderStateRecord orderStateRecord = new OrderStateRecord();
        orderStateRecord.setOrderId(orderId);
        orderStateRecord.setOrderBeforeState(beforeState);
        orderStateRecord.setOrderAfterState(afterState);
        orderStateRecord.setRecordSource(recordSource);
        orderStateRecord.setOperatorId(operatorId);
        orderStateRecordService.insertOrderStateRecord(orderStateRecord);
    }

    public void addOrderStateRecord(String orderId, String beforeState, String afterState,String recordSource,String recordReason,Long operatorId) {
        OrderStateRecord orderStateRecord = new OrderStateRecord();
        orderStateRecord.setOrderId(orderId);
        orderStateRecord.setOrderBeforeState(beforeState);
        orderStateRecord.setOrderAfterState(afterState);
        orderStateRecord.setRecordSource(recordSource);
        orderStateRecord.setRecordReason(recordReason);
        orderStateRecord.setOperatorId(operatorId);
        orderStateRecordService.insertOrderStateRecord(orderStateRecord);
    }

    @Override
    public void reletAudit(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法续租！");
    }

    @Override
    public void exchangeHouse(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法换房！");
    }

    @Override
    public void exchangeHouseAudit(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法换房！");
    }

    @Override
    public void sublease(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法转租！");
    }

    @Override
    public void subleaseAudit(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法转租！");
    }

    @Override
    public void houseInspection(Order order) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法验房！");
    }

    @Override
    public void orderOvertime(Order order) {
        String label = getLabel(order);
        LOG.error("订单超时处理异常,当前订单状态为" + label + ",！");
    }

    @Override
    public void preOrderSignOvertime(Order order) {
        String label = getLabel(order);
        LOG.error("预订单超时处理异常,当前订单状态为" + label + ",！");
    }

    @Override
    public int orderStayCheckIn(Order order,String remark) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }

    @Override
    public int orderCancellation(Order order,String recordSource,String recordReason) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }

    @Override
    public int cancelReservation(Order order, String recordSource, String recordSource1) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }

    @Override
    public int orderCheckIn(Order order, String recordSource, Long userId) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }

    @Override
    public int unsubscribeSuccessfully(Order order, String recordSource, String recordReason) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }

    @Override
    public int orderCheckingOut(Order order, String recordSource, Long operatorId) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }

    @Override
    public int orderCompletion(Order order, String recordSource, Long operatorId) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }

    @Override
    public int unsubscribeFailureStayCheckIn(Order order, String remark) {
        String label = getLabel(order);
        throw new ServiceException("订单状态异常,当前订单状态为" + label + ",无法变更！");
    }
}
